package com.zhihao.takeaway.controller;


import com.alibaba.fastjson.JSON;
import com.zhihao.takeaway.common.utils.*;
import com.zhihao.takeaway.entity.Dish;
import com.zhihao.takeaway.entity.ShoppingCart;
import com.zhihao.takeaway.entity.User;
import com.zhihao.takeaway.service.AddressBookService;
import com.zhihao.takeaway.service.DishService;
import com.zhihao.takeaway.service.ShoppingCartService;
import com.zhihao.takeaway.service.UserService;
import org.elasticsearch.client.RestHighLevelClient;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DishService dishService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private Redisson redisson;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("/getUser")
    public Result getUser(){
        Result result = new Result();
        String UserId = getUserId();
        User user = userService.getUser(UserId);
        EasyUITable easyUITable = addressBookService.getAddressBookByUserId(UserId);
        user.put("addressBook",easyUITable);
        result.setData(user);
        result.success("成功查询");
        return result;
    }
//    @GetMapping("/getInitData")
//    public Result getInitData(Integer nownum,Integer wantnum){
//        Result result = new Result();
//        String UserId = getUserId();
//        if(UserId == null || UserId.equals("")){
//            result.fail("当前用户未登录");
//        }else{
//            String lockKey = "dish";
//            RLock redissonLock = redisson.getLock(lockKey);
//            try {
//                redissonLock.lock(10, TimeUnit.SECONDS);
//                EasyUITable easyUITable = pageHelperUtil.getList(nownum, wantnum);
//                result.setData(easyUITable);
//                result.success("成功查询");
//            } finally {
//                redissonLock.unlock();
//            }
//        }
//        return result;
//    }
    @GetMapping("/getSearchData")
    public Result getSearchData(Integer nownum,Integer wantnum,String key) throws Exception {
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            List<Map<String, Object>> dishs = dishService.searchPage(key, nownum, wantnum);
            result.setData(new EasyUITable(dishs.size(),dishs));
            result.success("成功查询");
        }
        return result;
    }

    @PostMapping("/saveShoppingCart")
    public Result saveShoppingCart(@RequestBody Dish dish){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.setImage("/images/food2.jpg");
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setDishId(dish.getId());
            shoppingCart.setUserId(UserId);
            shoppingCart.setCreateTime(DateTool.getCurrTime());
            Integer num = shoppingCartService.insert(shoppingCart);
            if(num == 0){
                result.fail("插入失败");
            }else{
                result.success("已加入购物车");
            }
        }
        return result;
    }
    @PostMapping("/getAddressAndDish")
    public Result getAddressAndDish(String dishId){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            EasyUITable address = addressBookService.getAddressBookByUserId(UserId);
            List<Dish> list = dishService.selectByIds(dishId);
            address.setEtc(list);
            result.setData(address);
        }
        return result;
    }


    public String getUserId(){
        String token = request.getHeader("token");
        String UserId = JwtUtil.validateToken(token);
        return UserId;
    }
}
